﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ATSModel;
using ATSBLL;

public partial class Login : System.Web.UI.Page
{
    private Role tRole;
    protected void Page_Load(object sender, EventArgs e)
    {  

    }
    protected void login_onclick(object sender, EventArgs e)
    {
        string usr = this.userId.Value;
        string pwd = this.password.Value; 
        string str = "";
        if (usr == "用户名" || pwd == "密码")
        {
            str = "请输入用户名或密码!";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str + "');</script>");
        }
        else if (IsNumberic(usr)==false)
        {           
            str = "用户名输入不正确!";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str + "');</script>");
        }
        else if (AuthenticateUser(usr, pwd) == false)
        {
            str = "密码输入不正确!";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str + "');</script>");
        }
        else
        {
            Response.Redirect("~/MainIndex.aspx");
        }

    }

    private bool IsNumberic(string oText)
    {
        try
        {
            int var1 = Convert.ToInt32(oText);
            return true;
        }
        catch
        {
            return false;
        }
    }


    /// <summary>
    /// 用户登录账号验证
    /// </summary>
    /// <param name="UserID"></param>
    /// <param name="Pwd"></param>
    /// <returns></returns>
    public bool AuthenticateUser(string UserID, string Pwd)
    {

        TRoleInfo tInfo = new TRoleInfo();
        tRole = new Role(); 
        tInfo = tRole.GetForUser(UserID);

        if (tInfo == null)
        {
            return false;
        }
        if (tInfo.Password == Pwd)
        {
            return true;
        }
        return false;
    }
}
